package com.nuanshui.heatedloan.order.integration.execute.handler.syshandler;

import com.nuanshui.heatedloan.middleware.msgcenter.bean.Model;
import com.nuanshui.heatedloan.msgcenter.integration.MsgTools;
import com.nuanshui.heatedloan.msgcenter.model.SmsPushMsgBody;
import com.nuanshui.heatedloan.order.integration.annotation.OrderExecuteAnnotation;
import com.nuanshui.heatedloan.order.integration.bean.OrderResult;
import com.nuanshui.heatedloan.order.integration.constant.OrderHandlerOrderNum;
import com.nuanshui.heatedloan.order.integration.execute.handler.OrderHandler;
import com.nuanshui.heatedloan.order.integration.form.OrderExecuteForm;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;

/**
 * 推送的 handler
 * @Author: Gonzo
 * @Date 2017/4/13下午6:50
 * @since 1.4.2
 */
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@OrderExecuteAnnotation(value="pushHandler", order= OrderHandlerOrderNum.ORDER_PUSH)
public class PushHandler implements OrderHandler {

    @Autowired
    private MsgTools msgTools;

    private OrderHandler nextOrderhandler;

    private Logger logger = LoggerFactory.getLogger("appointment_order");

    /**
     * 执行生成订单的一系列操作
     * @Author: Gonzo
     * @Date 2017/3/23下午6:41
     * @since 1.4.2
     */
    @Override
    public OrderResult placeOrder(OrderExecuteForm form, OrderResult result){

        nextOrderhandler.placeOrder(form, result);

        if (!result.isSuccess()) {
            return result;
        }


        pushMsg(result);

        return result;
    }

    /**
     * 订单支付成功
     * @Author: Gonzo
     * @Date 2017/4/13下午5:07
     * @since 1.4.2
     */
    @Override
    public OrderResult paySuccessOrder(OrderExecuteForm form, OrderResult result){
        nextOrderhandler.paySuccessOrder(form, result);
        if (!result.isSuccess()) {
            return result;
        }

        pushMsg(result);

        return result;
    }

    private void pushMsg(OrderResult result)
    {
        if (StringUtils.isEmpty(result.getPushBizId())
                || StringUtils.isEmpty(result.getPushBizType())
                || result.getSmsPushCodeTemp() == null) {

        }

        SmsPushMsgBody model = new SmsPushMsgBody();
        model.setBizType(result.getPushBizType());
        model.setTemplateCode(result.getSmsPushCodeTemp().gettempCode());

        msgTools.sendSmsMsg(model);
    }

    /**
     * 取消, 不做推送, 直接调用
     * @Author: Gonzo
     * @Date 2017/4/13下午5:53
     * @since 1.4.2
     */
    @Override
    public OrderResult cancelOrder(OrderExecuteForm form, OrderResult result){
        return nextOrderhandler.cancelOrder(form, result);
    }
    
    /**
     * 校验
     * @Author: Gonzo
     * @Date 2017/4/13下午5:54
     * @since 1.4.2
     */
    @Override
    public OrderResult verifyAppointmentOrder(OrderExecuteForm form, OrderResult result){
        return nextOrderhandler.verifyAppointmentOrder(form, result);
    }

    @Override
    public void setNextOrderHandler(OrderHandler orderHandler) {
        this.nextOrderhandler = orderHandler;
    }
}
